using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.ServiceFabric.Services.Communication.AspNetCore;
using Microsoft.ServiceFabric.Services.Communication.Runtime;
using Microsoft.ServiceFabric.Services.Runtime;
using Ocelot.Samples.Web;
using System;
using System.Collections.Generic;
using System.Fabric;
using System.IO;

namespace Ocelot.Samples.ServiceFabric.DownstreamService;

/// <summary>
/// The FabricRuntime creates an instance of this class for each service type instance. 
/// </summary>
internal sealed class ApiGateway : StatelessService
{
    public ApiGateway(StatelessServiceContext context)
        : base(context) { }

    /// <summary>
    /// Optional override to create listeners (like tcp, http) for this service instance.
    /// </summary>
    /// <returns>The collection of listeners.</returns>
    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        return new ServiceInstanceListener[]
        {
            new(serviceContext =>
                new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
                {
                    Console.WriteLine($"Starting Kestrel on {url}");
                    ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");
                    return OcelotHostBuilder.Create()
                        .UseKestrel()
                        .ConfigureServices(services => services.AddSingleton(serviceContext))
                        .UseContentRoot(Directory.GetCurrentDirectory())
                        .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.UseUniqueServiceUrl)
                        .UseStartup<Startup>()
                        .UseUrls(url)
                        .ConfigureLogging((hostingContext, logging) =>
                        {
                            logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                            logging.AddConsole();
                        })
                        .Build();
                }))
        };
    }
}
